<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/xrd.php';
require_once $basedir . '/../../../classes/xrd/childelement.php';
require_once $basedir . '/../../../classes/xrd/lrdd.php';
require_once $basedir . '/../../../classes/xrd/lrdd/alias.php';
require_once $basedir . '/../../../classes/xrd/lrdd/subject.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/atom.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/salmon.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/magicpublickey.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/empty.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/webfingerprofilepage.php';

class Xrd_LrddTest extends PHPUnit_Framework_TestCase {
    
    public function testCreationOfLrddFromArray() {
       
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:tkoski@boobs.com</Subject>
  <Alias>http://boobs.com/tkoski</Alias>
  <Link rel="http://webfinger.net/rel/profile-page" href="http://boobs.com/tkoski" type="text/html"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" href="http://boobs.com/updates-from/tuomas.atom" type="application/atom+xml"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://boobs.com/salmon/tuomas"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB"/>
</XRD>
';
        
        $array[Xrd_Lrdd::KEY_SUBJECT]           = 'acct:tkoski@boobs.com';
        $array[Xrd_Lrdd::KEY_ALIAS]             = 'http://boobs.com/tkoski';
        $array[Xrd_Lrdd::KEY_ATOM]              = 'http://boobs.com/updates-from/tuomas.atom';
        $array[Xrd_Lrdd::KEY_SALMON]            = 'http://boobs.com/salmon/tuomas';
        $array[Xrd_Lrdd::KEY_MAGIC_PUBLIC_KEY]  = 'RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB';
        $array[Xrd_Lrdd::KEY_WEBFINGER_PROFILE_PAGE] = 'http://boobs.com/tkoski';

        $obj = Xrd_Lrdd::buildFromArray($array);
        $this->assertEquals($expected, $obj->asXML());        
    }
    
    public function testCreationOfLrddFromRawXml() {
        $rawXml = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:tkoski@identi.ca</Subject>
  <Alias>http://identi.ca/user/52467</Alias>
  <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://identi.ca/user/52467"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/52467.atom"/>
  <Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://identi.ca/tkoski/hcard"/>
  <Link rel="http://gmpg.org/xfn/11" type="text/html" href="http://identi.ca/user/52467"/>
  <Link rel="describedby" type="application/rdf+xml" href="http://identi.ca/tkoski/foaf"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/52467"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/52467"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB"/>
  <Link rel="http://ostatus.org/schema/1.0/subscribe" template="http://identi.ca/main/ostatussub?profile={uri}"/>
</XRD>';
         
        $obj = Xrd_Lrdd::buildFromRawXML($rawXml);
        $this->assertEquals("http://identi.ca/api/statuses/user_timeline/52467.atom", $obj->getLinkAtom()->gethRef());
        $this->assertEquals("http://identi.ca/main/salmon/user/52467", $obj->getLinkSalmon()->gethRef());
        $this->assertEquals("data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB", $obj->getLinkMagicPublicKey()->gethRef());
        $this->assertEquals("http://identi.ca/user/52467", $obj->getLinkWebfingerProfilePage()->gethRef());
    }
    
    public function testNoWebfingerProfile() {
        $rawXml = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:tkoski@identi.ca</Subject>
  <Alias>http://identi.ca/user/52467</Alias>
  <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/52467.atom"/>
  <Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://identi.ca/tkoski/hcard"/>
  <Link rel="http://gmpg.org/xfn/11" type="text/html" href="http://identi.ca/user/52467"/>
  <Link rel="describedby" type="application/rdf+xml" href="http://identi.ca/tkoski/foaf"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/52467"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/52467"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB"/>
  <Link rel="http://ostatus.org/schema/1.0/subscribe" template="http://identi.ca/main/ostatussub?profile={uri}"/>
</XRD>';
         
        $obj = Xrd_Lrdd::buildFromRawXML($rawXml);
        $this->assertEquals("", $obj->getLinkWebfingerProfilePage()->gethRef());
    }
    
    public function testCreationOfLrddFromRawXmlGoogle() {
        $rawXml = "<?xml version='1.0'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
        <Subject>acct:koski.tuomas@gmail.com</Subject>
        <Alias>http://www.google.com/profiles/koski.tuomas</Alias>
        <Link rel='http://portablecontacts.net/spec/1.0' href='http://www-opensocial.googleusercontent.com/api/people/'/>
        <Link rel='http://portablecontacts.net/spec/1.0#me' href='http://www-opensocial.googleusercontent.com/api/people/117625412053868084488/'/>
        <Link rel='http://webfinger.net/rel/profile-page' href='http://www.google.com/profiles/koski.tuomas' type='text/html'/>
        <Link rel='http://microformats.org/profile/hcard' href='http://www.google.com/profiles/koski.tuomas' type='text/html'/>
        <Link rel='http://gmpg.org/xfn/11' href='http://www.google.com/profiles/koski.tuomas' type='text/html'/>
        <Link rel='http://specs.openid.net/auth/2.0/provider' href='http://www.google.com/profiles/koski.tuomas'/>
        <Link rel='describedby' href='http://www.google.com/profiles/koski.tuomas' type='text/html'/>
        <Link rel='describedby' href='http://www.google.com/s2/webfinger/?q=koski.tuomas%40gmail.com&amp;fmt=foaf' type='application/rdf+xml'/>
        <Link rel='http://schemas.google.com/g/2010#updates-from' href='https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@public' type='application/atom+xml'/>
</XRD>";
         
        $obj = Xrd_Lrdd::buildFromRawXML($rawXml);
        $this->assertEquals("https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@public", $obj->getLinkAtom()->gethRef());
        $this->assertEquals("", $obj->getLinkSalmon()->gethRef());
        $this->assertEquals("", $obj->getLinkMagicPublicKey()->gethRef());
        
    }
    
    
}